/*************************************************************************
 *                                                                       *
 * This file is part of the FreeMAPs distribution.  For full copyright   *
 * information, see COPYRIGHT and COPYING.LESSER.                        *
 *                                                                       *
 * Author:           sqbang                                              *
 * Email:            sqbang@ustc.edu                                     *
 * Date:             2012.02.14                                          *
 *                                                                       *
 *************************************************************************/

//declaration header
#include "COMMON/tbox/Logger.h"

//COMMON headers
#include "COMMON/tbox/PIO.h"

namespace DOGOS
{

Logger * Logger::s_instance = 0;

StartupShutdownManager::Handler
Logger::s_finalize_handler(
    0,
    0,
    0,
    Logger::finalize_callback,
    StartupShutdownManager::priorityLogger);



class AbortAppender : public Logger::Appender
{
    void
    logMessage(
        const std::string& message,
        const std::string& file_name,
        const int line);
};



class WarningAppender : public Logger::Appender
{
    void
    logMessage(
        const std::string& message,
        const std::string& file_name,
        const int line);
};



class DebugAppender:public Logger::Appender
{

    void
    logMessage(
        const std::string& message,
        const std::string& file_name,
        const int line);
};



void
AbortAppender::logMessage(
    const std::string& message,
    const std::string& file_name,
    const int line)
{
    PERR << "Program abort called in file \"" << file_name
         << "\" at line " << line << std::endl;
    PERR << "ERROR MESSAGE: " << std::endl << message.c_str() << std::endl;
    PERR << std::flush;
}



void
WarningAppender::logMessage(
    const std::string& message,
    const std::string& file_name,
    const int line)
{
    PLOG << "Warning in file \"" << file_name
         << "\" at line " << line << std::endl;
    PLOG << "WARNING MESSAGE: " << std::endl << message.c_str() << std::endl;
    PLOG << std::flush;
}



void
DebugAppender::logMessage(
    const std::string& message,
    const std::string& file_name,
    const int line)
{
    PLOG << "Debug in file \"" << file_name
         << "\" at line " << line << std::endl;
    PLOG << "DEBUG MESSAGE: " << std::endl << message.c_str() << std::endl;
    PLOG << std::flush;
}



Logger::Logger():
    d_log_warning(true),
    d_log_debug(false)
{
    /*
     * Initializers for default logging methods.
     */
    d_abort_appender = new AbortAppender();

    d_warning_appender = new WarningAppender();

    d_debug_appender = new DebugAppender();

}



Logger::~Logger()
{
    if (d_abort_appender)
    {
        delete d_abort_appender;
        d_abort_appender = 0;
    }

    if (d_warning_appender)
    {
        delete d_warning_appender;
        d_warning_appender = 0;
    }

    if (d_debug_appender)
    {
        delete d_debug_appender;
        d_debug_appender = 0;
    }
}



void
Logger::finalize_callback()
{
    if (s_instance)
    {
        delete s_instance;
        s_instance = 0;
    }
}



Logger*
Logger::get_instance()
{
    if (s_instance == 0)
    {
        s_instance = new Logger();
    }

    return s_instance;
}



Logger::Appender::~Appender()
{
    /*
     * do nothing
     */
}

} //namespace FreeMAPs
